﻿namespace GSOrganization.Application;

/// <summary>
/// 日志
/// </summary>
public class SysLogService : ISysLogService, ITransient, Furion.DynamicApiController.IDynamicApiController
{

    private readonly IRepository<SysLog> _syslog;


    public SysLogService(IRepository<SysLog> syslog)
    {
        _syslog = syslog;

    }


    /// <summary>
    /// 列表
    /// </summary>
    /// <param name="param">列表的参数对象</param>
    /// <returns>列表返回值</returns>
    [HttpPost]
    public ResponseModel<List<SysLogIndexOutput>> DataSysLog(SysLogIndexInput param)
    {

        var data = _syslog.AsQueryable();
        data = data.Where(w => w.IsDel == false);

        if (!string.IsNullOrWhiteSpace(param.Name))
        {

            data = data.Where(a => a.Name.Contains(param.Name));
        }

        if (!string.IsNullOrWhiteSpace(param.CreatedTimeStartDate))
        {
            var start = Convert.ToDateTime(param.CreatedTimeStartDate);
            data = data.Where(a => a.CreatedTime >= start);
        }

        if (!string.IsNullOrWhiteSpace(param.CreatedTimeEndDate))
        {
            var end = Convert.ToDateTime(param.CreatedTimeEndDate).AddDays(1);
            data = data.Where(a => a.CreatedTime < end);
        }

        var result = data.OrderByDescending(o => o.CreatedTime)
       .Skip((param.Page - 1) * param.Limit)
       .Take(param.Limit)
       .Select(s => new SysLogIndexOutput
       {
           Id = s.Id,
           Name = s.Name,
           Message = s.Message,
           Result = s.Result,
           Ip = s.Ip,
           MenuId = s.MenuId,
           CreatedTime = s.CreatedTime.ToString(),
       });


        var total = data.Count();

        return new ResponseModel<List<SysLogIndexOutput>>
        {
            count = total,
            data = result.ToList()
        };

    }



}



